package com.clkj.nangang.mvp.index.newsdetail;

import android.os.Bundle;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.clkj.nangang.R;
import com.clkj.nangang.app.CustomApplication;
import com.clkj.nangang.mvp.base.BaseActivity;
import com.clkj.nangang.mvp.model.NewsDetailModel;
import com.clkj.nangang.util.CommonUtil;
import com.clkj.nangang.util.schedulers.SchedulerProvider;

public class NewsDetailActivity extends BaseActivity implements NewsDetailContract.View {

    private TextView tvNewstitle;
    private TextView tvTime;
    private WebView wvContent;

    private String newsId;

    private NewsDetailContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        initTitle(null, null, "详情", true, null);
        initData();
        initView();
        assignView();

        //获取详情
        presenter.getNewsDetail(newsId);
    }

    @Override
    public void initData() {
        newsId = getIntent().getStringExtra("newsId");
        presenter = new NewsDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

    }

    @Override
    public void assignView() {

    }

    @Override
    public void initView() {

        tvNewstitle = (TextView) findViewById(R.id.tv_newstitle);
        tvTime = (TextView) findViewById(R.id.tv_time);
        wvContent = (WebView) findViewById(R.id.wv_content);
    }

    @Override
    public void onNetError(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getNewsDetailSuccess(NewsDetailModel newsDetailModel) {

        tvNewstitle.setText(newsDetailModel.getTitle());
        tvTime.setText(newsDetailModel.getFBTime());

        String htmlData = newsDetailModel.getNewsContent();

        CommonUtil.initWebView(wvContent);

        wvContent.loadData(htmlData, "text/html; charset=UTF-8", null);
    }

    @Override
    public void getNewsDetailFail(String tip) {
        Toast.makeText(this, tip, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(NewsDetailContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unsubscribe();
    }
}
